package com.cqw.robot.utils;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TessAPI;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.Word;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.Size;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * 根据文本查找图片上的位置
 */
@Slf4j
public class TextDetection {

    public static void main(String[] args) {

//        System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 加载 OpenCV 库

//        String userDir = System.getProperty("user.dir");
//        String targetImg = userDir + "/images/8file.png";
//        TextRecognition(targetImg);

//        TextRecognition();
        Point point = textBoundingBox("路");
        log.info("查找到的文字位置：{}",point);
    }


    /** 根据文本查找图片（截屏）上的位置 **/
    public static Point textBoundingBox(String text) {
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath(System.getProperty("user.dir") + "\\traineddata");
        tesseract.setLanguage("chi_sim"); // 设置识别语言
        // 识别文本并获取边界框的信息
        List<Word> words = tesseract.getWords(getFullScreenShot(), TessAPI.TessPageIteratorLevel.RIL_WORD);


        for (Word word : words) {
            log.info("words:{}",word.getText());
            if(text.indexOf(word.getText()) != -1){
                Rectangle rectangle = word.getBoundingBox();
                return rectangle.getLocation();
            }
        }
        return null;
    }


    public static void ImagePreprocessing(String targetImg) {
        // 读取图像
        Mat image = Imgcodecs.imread(targetImg);

        // 将图像转换为灰度图
        Mat grayImage = new Mat();
        Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);

        // 应用高斯模糊
        Mat blurredImage = new Mat();
        Imgproc.GaussianBlur(grayImage, blurredImage, new Size(5, 5), 0);
    }

    public static void TextRecognition() {
        Tesseract tesseract = new Tesseract();
        tesseract.setDatapath("D:\\traineddata"); // 设置 tessdata 路径
        tesseract.setLanguage("chi_sim"); // 设置识别语言
        try {
            // 从图像中提取文本
            String resultText = tesseract.doOCR(getFullScreenShot());
            System.out.println("识别的文本是: " + resultText);
        } catch (TesseractException e) {
            e.printStackTrace();
        }
    }


    /**
     * 全屏截图
     * @return 返回BufferedImage
     */
    public static BufferedImage getFullScreenShot() {
        BufferedImage bfImage = null;
        int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        try {
            Robot robot = new Robot();
            bfImage = robot.createScreenCapture(new Rectangle(0, 0, width, height));
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return bfImage;
    }

}